package com.ruoyi.server.api.memberTypeManagement.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.ObjectUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.exception.errorCode.CommonException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.validator.ValidatorUtil;
import com.ruoyi.server.api.memberTypeManagement.domain.MemberTypeManagement;
import com.ruoyi.server.api.memberTypeManagement.mapper.MemberTypeManagementMapper;
import com.ruoyi.server.api.memberTypeManagement.service.IMemberTypeManagementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import static jdk.nashorn.internal.runtime.regexp.joni.Config.log;

/**
 * 会员类型Service业务层处理
 *
 * @author ruoyi
 * @date 2024-06-10
 */
@Service
public class MemberTypeManagementServiceImpl implements IMemberTypeManagementService {
    @Autowired
    private MemberTypeManagementMapper memberTypeManagementMapper;

    /**
     * 查询会员类型
     *
     * @param id 会员类型主键
     * @return 会员类型
     */
    @Override
    public MemberTypeManagement selectMemberTypeManagementById(Integer id, String venueUniqueIdentifier) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        venueUniqueIdentifier = (loginUser.getUser().getVenueUniqueIdentifier());
        return memberTypeManagementMapper.selectMemberTypeManagementById(id, venueUniqueIdentifier);
    }

    /**
     * 查询会员类型列表
     *
     * @param memberTypeManagement 会员类型
     * @return 会员类型
     */
    @Override
    public List<MemberTypeManagement> selectMemberTypeManagementList(MemberTypeManagement memberTypeManagement) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        memberTypeManagement.setVenueUniqueIdentifier(loginUser.getUser().getVenueUniqueIdentifier());
        return memberTypeManagementMapper.selectMemberTypeManagementList(memberTypeManagement);
    }

    /**
     * 新增会员类型
     *
     * @param memberTypeManagement 会员类型
     * @return 结果
     */
    @Override
    public int insertMemberTypeManagement(MemberTypeManagement memberTypeManagement) throws CommonException {
        // 验证对象
        if (memberTypeManagement==null) {
            return 0;
        }
        // 获取登录用户信息
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser == null || loginUser.getUser() == null) {
            throw new CommonException("无法获取登录用户信息");
        }
        // 设置额外信息
        memberTypeManagement.setVenueUniqueIdentifier(loginUser.getUser().getVenueUniqueIdentifier());
        memberTypeManagement.setCreateTime(DateUtils.getNowDate());
        memberTypeManagement.setUpdateTime(new Date());
        memberTypeManagement.setEnableStatus(1L);
        memberTypeManagement.setDelFlag(0L);
        int result = memberTypeManagementMapper.insertMemberTypeManagement(memberTypeManagement);
        if (result <= 0) {
            throw new CommonException("更新失败");
        }
        return result;
    }

    /**
     * 修改会员类型
     *
     * @param memberTypeManagement 会员类型
     * @return 结果
     */
    @Override
    public int updateMemberTypeManagement(MemberTypeManagement memberTypeManagement) throws CommonException {
        // 验证对象
        if (!ValidatorUtil.ValidationObject(memberTypeManagement)) {
            return 0;
        }
        // 获取登录用户信息
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser == null || loginUser.getUser() == null) {
            throw new CommonException("无法获取登录用户信息");
        }
        // 设置额外信息
        memberTypeManagement.setVenueUniqueIdentifier(loginUser.getUser().getVenueUniqueIdentifier());
        memberTypeManagement.setUpdateTime(DateUtils.getNowDate());
        // 执行数据库更新操作
        int result = memberTypeManagementMapper.updateMemberTypeManagement(memberTypeManagement);
        if (result <= 0) {
            throw new CommonException("更新失败");
        }
        return result;
    }

    /**
     * 修改会员卡的状态
     *
     * @param memberTypeManagement 会员类型
     * @return 结果
     */
    @Override
    public int updateMemberTypeManagementState(MemberTypeManagement memberTypeManagement) throws CommonException {
        // 验证对象
        if (!ValidatorUtil.ValidationObject(memberTypeManagement)) {
            return 0;
        }
        // 获取登录用户信息
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser == null || loginUser.getUser() == null) {
            throw new CommonException("无法获取登录用户信息");
        }
        // 设置额外信息
        memberTypeManagement.setVenueUniqueIdentifier(loginUser.getUser().getVenueUniqueIdentifier());
        memberTypeManagement.setUpdateTime(DateUtils.getNowDate());
        // 执行数据库更新操作
        int result = memberTypeManagementMapper.updateMemberTypeManagementState(memberTypeManagement);
        if (result <= 0) {
            throw new CommonException("更新状态失败");
        }
        return result;
    }

    /**
     * 删除会员类型信息
     *
     * @param memberTypeManagement 会员类型主键
     * @return 结果
     */
    @Override
    public int deleteMemberTypeManagementById(MemberTypeManagement memberTypeManagement) throws CommonException {
        // 验证对象
        if (!StringUtils.isNotNull(memberTypeManagement.getId())) {
            return 0;
        }
        // 获取登录用户信息
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser == null || loginUser.getUser() == null) {
            throw new CommonException("无法获取登录用户信息");
        }
        // 设置额外信息
            memberTypeManagement.setVenueUniqueIdentifier(loginUser.getUser().getVenueUniqueIdentifier());
        int result = memberTypeManagementMapper.updateMemberTypeManagementDelFlagById(memberTypeManagement);
        if (result <= 0) {
            throw new CommonException("更新状态失败");
        }
        return result;
    }
}
